package com.group2.edu.realtime.dws;/**
 * @Author: Wxs
 * @Description: TODO
 * @Date: 2024/12/16 10:18
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.group2.edu.realtime.common.base.BaseApp;
import com.group2.edu.realtime.common.bean.TrafficHomeCourse_listCourse_detailPageViewBean;
import com.group2.edu.realtime.common.constant.Constant;
import com.group2.edu.realtime.common.function.BeanToJsonStrMapFunction;
import com.group2.edu.realtime.common.util.DateFormatUtil;
import com.group2.edu.realtime.common.util.FlinkSinkUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @Author: Wxs
 * @Description: TODO
 * @Date: 2024/12/16 10:18
 * 需要启动的进程：zk、kafka、flume、doris、DwdBaseLog、DwsTrafficHomeCourse_listCourse_detailPageViewWindow
 */
public class DwsTrafficHomeCourse_listCourse_detailPageViewWindow extends BaseApp {

    public static void main(String[] args) {
        new DwsTrafficHomeCourse_listCourse_detailPageViewWindow().start(
                10023,
                4,
                "DwsTrafficHomeCourse_listCourse_detailPageViewWindow",
                Constant.TOPIC_DWD_TRAFFIC_PAGE
        );
    }
    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> kafkaStrDS) {
        //TODO 对流中数据类型进行转换 jsonStr -> jsonObj
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaStrDS.map(JSON::parseObject);
        //TODO 过滤出 page_id 为 home、course_list或course_detail 的数据
        SingleOutputStreamOperator<JSONObject> filterDS = jsonObjDS.filter(
                new FilterFunction<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject jsonObj) throws Exception {
                        String pageId = jsonObj.getJSONObject("page").getString("page_id");
                        return "home".equals(pageId) || "course_list".equals(pageId) || "course_detail".equals(pageId);
                    }
                }
        );
        //TODO 指定watermark的生成策略以及提取事件时间字段
        SingleOutputStreamOperator<JSONObject> withWatermarkDS = filterDS.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forMonotonousTimestamps()
                .withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
                    @Override
                    public long extractTimestamp(JSONObject element, long recordTimestamp) {
                        return element.getLong("ts");
                    }
                })
        );
        //TODO 按照mid分组
        KeyedStream<JSONObject, String> keyedDS = withWatermarkDS.keyBy(
                new KeySelector<JSONObject, String>() {
                    @Override
                    public String getKey(JSONObject value) throws Exception {
                        return value.getJSONObject("common").getString("mid");
                    }
                }
        );
        //TODO 使用flink状态编程，判断是否是那几个页面的独立访客 jsonObj -> 统计的实体类对象
        SingleOutputStreamOperator<TrafficHomeCourse_listCourse_detailPageViewBean> processDS = keyedDS.process(
                new KeyedProcessFunction<String, JSONObject, TrafficHomeCourse_listCourse_detailPageViewBean>() {
                    private ValueState<String> homeLastVisitDateState;
                    private ValueState<String> courseListLastVisitDateState;
                    private ValueState<String> courseDetailLastVisitDateState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        ValueStateDescriptor<String> homeStateProperties = new ValueStateDescriptor<String>("homeLastVisitDateState", String.class);
                        homeStateProperties.enableTimeToLive(StateTtlConfig.newBuilder(Time.days(1)).build());
                        homeLastVisitDateState = getRuntimeContext().getState(homeStateProperties);

                        ValueStateDescriptor<String> courseListStateProperties = new ValueStateDescriptor<String>("courseListLastVisitDateState", String.class);
                        courseListStateProperties.enableTimeToLive(StateTtlConfig.newBuilder(Time.days(1)).build());
                        courseListLastVisitDateState = getRuntimeContext().getState(courseListStateProperties);

                        ValueStateDescriptor<String> courseDetailStateProperties = new ValueStateDescriptor<String>("courseDetailLastVisitDateState", String.class);
                        courseDetailStateProperties.enableTimeToLive(StateTtlConfig.newBuilder(Time.days(1)).build());
                        courseDetailLastVisitDateState = getRuntimeContext().getState(courseDetailStateProperties);

                    }

                    @Override
                    public void processElement(JSONObject jsonObj, KeyedProcessFunction<String, JSONObject, TrafficHomeCourse_listCourse_detailPageViewBean>.Context ctx, Collector<TrafficHomeCourse_listCourse_detailPageViewBean> out) throws Exception {
                        //获取页面id
                        String pageId = jsonObj.getJSONObject("page").getString("page_id");
                        //获取当前日期
                        Long ts = jsonObj.getLong("ts");
                        String curVisitDate = DateFormatUtil.tsToDate(ts);
                        Long homeUvCt = 0L;
                        Long courseListUvCt = 0L;
                        Long courseDetailUvCt = 0L;
                        if ("home".equals(pageId)) {
                            //首页
                            //从状态中获取当前设备上次访问首页的日期
                            String lastVisitDate = homeLastVisitDateState.value();
                            if (StringUtils.isEmpty(lastVisitDate) || !curVisitDate.equals(lastVisitDate)) {
                                homeUvCt = 1L;
                                homeLastVisitDateState.update(curVisitDate);
                            }
                        } else if ("course_list".equals(pageId)) {
                            String lastVisitDate = courseListLastVisitDateState.value();
                            if (StringUtils.isEmpty(lastVisitDate) || !curVisitDate.equals(lastVisitDate)) {
                                courseListUvCt = 1L;
                                courseListLastVisitDateState.update(curVisitDate);
                            }
                        } else {
                            String lastVisitDate = courseDetailLastVisitDateState.value();
                            if (StringUtils.isEmpty(lastVisitDate) || !curVisitDate.equals(lastVisitDate)) {
                                courseDetailUvCt = 1L;
                                courseDetailLastVisitDateState.update(curVisitDate);
                            }
                        }
                        if (homeUvCt > 0l || courseListUvCt > 0l || courseDetailUvCt > 0l){
                            out.collect(new TrafficHomeCourse_listCourse_detailPageViewBean(
                                    "",
                                    "",
                                    "",
                                    homeUvCt,
                                    courseListUvCt,
                                    courseDetailUvCt,
                                    ts
                            ));
                        }


                    }
                }
        );
        //TODO 开窗
        AllWindowedStream<TrafficHomeCourse_listCourse_detailPageViewBean, TimeWindow> windowDS = processDS.windowAll(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10)));
        //TODO 聚合
        SingleOutputStreamOperator<TrafficHomeCourse_listCourse_detailPageViewBean> reduceDS = windowDS.reduce(
                new ReduceFunction<TrafficHomeCourse_listCourse_detailPageViewBean>() {
                    @Override
                    public TrafficHomeCourse_listCourse_detailPageViewBean reduce(TrafficHomeCourse_listCourse_detailPageViewBean value1, TrafficHomeCourse_listCourse_detailPageViewBean value2) throws Exception {
                        value1.setHomeUvCt(value1.getHomeUvCt() + value2.getHomeUvCt());
                        value1.setCourse_listUvCt(value1.getCourse_listUvCt() + value2.getCourse_listUvCt());
                        value1.setCourse_detailUvCt(value1.getCourse_detailUvCt() + value2.getCourse_detailUvCt());
                        return value1;

                    }
                }, new AllWindowFunction<TrafficHomeCourse_listCourse_detailPageViewBean, TrafficHomeCourse_listCourse_detailPageViewBean, TimeWindow>() {
                    @Override
                    public void apply(TimeWindow window, Iterable<TrafficHomeCourse_listCourse_detailPageViewBean> values, Collector<TrafficHomeCourse_listCourse_detailPageViewBean> out) throws Exception {
                        TrafficHomeCourse_listCourse_detailPageViewBean viewBean = values.iterator().next();
                        viewBean.setStt(DateFormatUtil.tsToDateTime(window.getStart()));
                        viewBean.setEdt(DateFormatUtil.tsToDateTime(window.getEnd()));
                        viewBean.setCurDate(DateFormatUtil.tsToDate(window.getStart()));
                        out.collect(viewBean);

                    }
                }
        );

        //TODO 将聚合结果写入doris中
      reduceDS.print();
      reduceDS.map(new BeanToJsonStrMapFunction<>())
              .sinkTo(FlinkSinkUtil.getDorisSink("dws_traffic_home_course_list_course_detail_page_view_window"));


    }
}
